<?php
/*
*用於開發者後臺的控制器初始化 By yan
*/
namespace yan\backendclient\controllers;
use yii\web\Controller;
use yii\filters\VerbFilter;
use Yii;

use yii\web\NotAcceptableHttpException;
class DebugController extends Controller
{
	public $layout = '@yan/backendclient/views/layouts/debug';
	
	// 初使用化 VerbFilter action過濾配置
    public $defaultVerFilterAction = [
                    'delete' => ['post'],
                ];
    // 初使用化 AccessControl rules過濾配置
    public $defaultAccessRules = [
            [
                'actions' => ['index', 'create', 'update','delete', 'view'],
                'allow' => true,
                'roles' => ['@'],
            ]
        ];
    public function behaviors()
    {
        $defaultVerFilterAction = $this->defaultVerFilterAction;
        $defaultAccessRules = $this->defaultAccessRules;
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => array_merge( $defaultVerFilterAction, $this->verbsActions() ),
            ],
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'rules' => array_merge( $defaultAccessRules, $this->accessRules() ),
            ],
        ];
    }
    
	public function init(){
		// $action_id = $this->action->id;
        // 時區設置
		parent::init();

	}

    public function beforeAction($action){

        $flag = parent::beforeAction($action);

        if( Yii::$app->user->isGuest ){
            $flag = false;
        }else if( !Yii::$app->user->identity->isAdmin() ){
            throw new NotAcceptableHttpException(Yii::t('debug', 'Access deny!'));
        }
        return $flag;
    }
    
    // 擴展的action verbs
    protected function verbsActions(){
        return [];
    }
    // 擴展的access rules
    protected function accessRules(){
        return [];
    }
}